layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 钉钉;前三十天，人员出勤数据管理
     */
    var DgdDingdingDayAttend = {
        tableId: "dgdDingdingDayAttendTable"
    };

    /**
     * 初始化表格的列
     */
    DgdDingdingDayAttend.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {type: 'numbers',align:'center', title: '序号', fixed:'left'},
            {field: 'id', hide: true, title: '主键;主键id'},
            {field: 'empId', title: '员工id', fixed:'left'},
            {field: 'empName', title: '员工姓名', fixed:'left'},
            {field: 'attendDay', title: '出勤日', fixed:'left',width: 130, templet: function (d) {
                    if (d.attendDay != '') {
                        return d.attendDay.substring(0,10);
                    }
                }},
            {field: 'attendWorkDay', fixed:'left', title: '出勤天数'},
            {field: 'timeOne', title: '打卡一', templet: function (d) {
                    if (d.timeOne != '') {
                        return d.timeOne.substring(10, 16);
                    }else {
                        return "空";
                    }
                }},
            {field: 'timeTwo', title: '打卡二', templet: function (d) {
                    if (d.timeTwo != '') {
                        return d.timeTwo.substring(10, 16);
                    }else {
                        return "空";
                    }
                }},
            {field: 'timeThree', title: '打卡三', templet: function (d) {
                    if (d.timeThree != '') {
                        return d.timeThree.substring(10, 16);
                    }else {
                        return "空";
                    }
                }},
            {field: 'timeFour', title: '打卡四', templet: function (d) {
                    if (d.timeFour != '') {
                        return d.timeFour.substring(10, 16);
                    }else {
                        return "空";
                    }
                }},
            {field: 'timeFive', title: '打卡五', templet: function (d) {
                    if (d.timeFive != '') {
                        return d.timeFive.substring(10, 16);
                    }else {
                        return "空";
                    }
                }},
            {field: 'timeSix', title: '打卡六', templet: function (d) {
                    if (d.timeSix != '') {
                        return d.timeSix.substring(10, 16);
                    }else {
                        return "空";
                    }
                }},
            {field: 'attendStatus', title: '打卡状态', templet: function (d) {
                    if (d.attendStatus == '0') {
                        return "正常";
                    }else if (d.attendStatus == '1'){
                        return "异常";
                    }
                }},
            {field: 'attendWorkMinutes', title: '出勤分钟数'},
            // {field: 'lateStatus', sort: true, title: '当天是否迟到;0.正常，1.标记迟到'},
            // {field: 'lateMinutes', sort: true, title: '当天迟到分钟;当天迟到分钟数'},
            // {field: 'leaveEarlyStatus', sort: true, title: '当天是否早退;0.正常，1.标记早退'},
            // {field: 'leaveEarlyMinutes', sort: true, title: '当天早退分钟;当天早退分钟数'},
            {field: 'lackAttendNum', title: '漏打卡次数'},
            // {field: 'requestStatus', title: '关联申请单', templet: function (d) {
            //         if (d.delFlag == '0') {
            //             return "无";
            //         }else if (d.delFlag == '1'){
            //             return "有";
            //         }
            //     }},
            // {field: 'requestOutStatus', title: '外出申请单', templet: function (d) {
            //         if (d.requestOutStatus == '0') {
            //             return "无";
            //         }else if (d.requestOutStatus == '1'){
            //             return "有";
            //         }
            //     }},
            {field: 'requestOutMinutes', title: '外出表单时间'},
            {field: 'requestNote', title: '申请单详情'},
            // {field: 'attendOut', title: '出差时长'},
            {field: 'requestOut', title: '外出时长'},
            // {field: 'attendShiftNote', title: '班次'},
            {field: 'workTime', title: '工作时长分钟'},
            // {field: 'delFlag', title: '删除标志', templet: function (d) {
            //         if (d.delFlag == '0') {
            //             return "正常";
            //         }else if (d.delFlag == '1'){
            //             return "删除";
            //         }
            //     }},
            {field: 'attendShiftNote', title: '班次'},
            {field: 'timeOneNote', title: '第一次打卡备注'},
            {field: 'timeTwoNote', title: '第二次打卡备注'},
            {field: 'timeThreeNote', title: '第三次打卡备注'},
            {field: 'timeFourNote', title: '第四次打卡备注'},
            {field: 'timeFiveNote', title: '第五次打卡备注'},
            {field: 'timeSixNote', title: '第六次打卡备注'},
            {field: 'createTime', title: '创建时间'},
            // {field: 'createUser', sort: true, title: '创建人;创建人id'},
            // {field: 'updateTime', sort: true, title: '更新时间;更新时间'},
            // {field: 'updateUser', sort: true, title: '更新人;更新人id'},
            // {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    DgdDingdingDayAttend.search = function () {
        var queryData = {};


        table.reload(DgdDingdingDayAttend.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    DgdDingdingDayAttend.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/btyDingdingDayAttend/add'
    };

    /**
    * 跳转到编辑页面
    *
    * @param data 点击按钮时候的行数据
    */
    DgdDingdingDayAttend.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/btyDingdingDayAttend/edit?id=' + data.id
    };

    /**
     * 导出excel按钮
     */
    DgdDingdingDayAttend.exportExcel = function () {
        var checkRows = table.checkStatus(DgdDingdingDayAttend.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    DgdDingdingDayAttend.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyDingdingDayAttend/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(DgdDingdingDayAttend.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    var attendYearMonth = $("#attendYearMonth").val();
    var empId = $("#empId").val();

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + DgdDingdingDayAttend.tableId,
        url: Feng.ctxPath + '/btyDingdingDayAttend/list',
        page: false,
        height: "full-158",
        cellMinWidth: 100,
        where: {attendYearMonth: attendYearMonth, empId: empId},
        cols: DgdDingdingDayAttend.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        DgdDingdingDayAttend.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    DgdDingdingDayAttend.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        DgdDingdingDayAttend.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + DgdDingdingDayAttend.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            DgdDingdingDayAttend.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            DgdDingdingDayAttend.onDeleteItem(data);
        }
    });
});
